//
//  JPRequest.m
//  GrainAndOil
//
//  Created by xiangming on 14-7-9.
//  Copyright (c) 2014年 JunePartner. All rights reserved.
//

#import "Request.h"

@implementation Request

//初始化网络请求和设置参数
- (id)initWithParamDic:(NSMutableDictionary *)paramDic
          andApiPrefix:(NSString *)prefix
          andApiSuffix:(NSString *)suffix
         andHttpMethod:(NSString *)httpMethod
       andRequestCache:(BOOL)requestCache
        andUseParamDic:(BOOL)useParamDic
{
    self = [super init];
    if (self) {
        _params=paramDic;
        _prefix = prefix;
        _useParamDic = useParamDic;
         NSString *paramSuffix = @"";
        if(!useParamDic&&paramDic!= nil){
            for (NSString *key in paramDic) {
                paramSuffix = [paramSuffix stringByAppendingFormat:@"%@=%@&",key,[paramDic objectForKey:key]];
            }
            if(paramSuffix.length>0)
            paramSuffix = [paramSuffix substringWithRange:NSMakeRange(0, paramSuffix.length-1)];
        }
        _suffix = [suffix stringByAppendingString:paramSuffix];
        _httpMethod = httpMethod;
        self.requestCache = requestCache;
    }
    return self;
    
}

//接口前缀相当于BASE_URL
- (NSString *)apiURLPrefix {
    return _prefix;
}

//接口后缀
- (NSString *)apiURLSuffix {
    return _suffix;
}

//网络请求方式
- (NSString *)apiRequestMethod {
    return _httpMethod;
}

//网络请求响应
- (Class)responseClass {
    return [BYResponse class];
}


//设置网络请求参数
- (NSMutableDictionary *)parameters {
    if(self.useParamDic){
    NSMutableDictionary *parametersDic = [super parameters];
    [parametersDic removeAllObjects];
    NSArray *allKeys = [_params allKeys];
    for(NSString *key in allKeys){
        id value = [_params objectForKey:key];
        [parametersDic setValue:value forKey:key];
    }
    return parametersDic;
    }
    else{
         return nil;
    }
}


@end
